﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MapEntities;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;
using DALEntities;

namespace WebApp.Controls
{
    public partial class ShapeVisualizationEditor : BaseDataEditor<ShapeItemVisualization>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                foreach (string xcolor in Enum.GetNames(typeof(ShapeBackgroundVisualizationMode)))
                {
                    ctlColoringType.Items.Add(new ListItem(xcolor));
                }
                foreach (string xdatatype in Enum.GetNames(typeof(DataType)))
                {
                    switch (xdatatype)
                    {
                        case "Blob": { continue; }
                        case "Spatial": { continue; }
                        case "Unknown": { continue; }
                    }
                    ctlRangeDataType.Items.Add(new ListItem(xdatatype));
                }
            }
        }

        public override void Edit(ShapeItemVisualization item)
        {
            base.Edit(item);
        }

        protected void ctlColoringType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ctlColoringType.SelectedIndex)
            {
                case 0:
                    {
                        ctlAutomaticPanel.Visible = true;
                        ctlDefiningPanel.Visible = false;
                        Current.BackgroundVisialization.RangeList.Clear();
                        ctlRangeList.Items.Clear();
                        ctlDefaultShapeColoring.ClearEditor();
                        ctlColoringInfoEditorRanges.ClearEditor();
                        ctlRangeStart.Text = string.Empty;
                        ctlRangeEnd.Text = string.Empty;
                        break;
                    }
                case 1:
                    {
                        ctlAutomaticPanel.Visible = false;
                        ctlDefiningPanel.Visible = true;
                        break;
                    }

            }
        }
        public override ShapeItemVisualization EndEdit()
        {
            ShapeItemVisualization xresult = base.EndEdit();
            ClearEditor();
            ctlRangeList.Items.Clear();
            ClearCurrent();
            return xresult;

        }

        protected override void ShowEntity(ShapeItemVisualization entity)
        {
            ctlRangeList.Items.Clear();

            //ctlDefaultColor.SelectedIndex = ctlDefaultColor.Items.IndexOf(ctlDefaultColor.Items.FindByText(entity.BackgroundVisialization.RangeList.DefaulColor.ToString()));
            ctlRangeDataType.SelectedIndex = ctlRangeDataType.Items.IndexOf(ctlRangeDataType.Items.FindByText(entity.BackgroundVisialization.DataType.ToString()));
            ctlColoringType.SelectedIndex = ctlColoringType.Items.IndexOf(ctlColoringType.Items.FindByText(entity.BackgroundVisialization.Mode.ToString()));
            ctlColoringType_SelectedIndexChanged(ctlColoringType, new EventArgs());
            if (entity.BackgroundVisialization.Mode == ShapeBackgroundVisualizationMode.Automatic)
            {
                ctlColoringInfoEditorAutomatic.Edit(entity.BackgroundVisialization.Automatic.ColoringInfo);
            }
            else
            {
                if (entity.BackgroundVisialization.RangeList.Count > 0)
                {
                    foreach (ShapeBackgroundRange xitem in entity.BackgroundVisialization.RangeList.List)
                    {                        
                   ctlRangeList.Items.Add(xitem.Start.ToString() + " - " + xitem.Finish.ToString());
                    }
                    ctlRangeList.SelectedIndex = 0;
                    showSelectedEntity(0, entity);
                    ctlColoringInfoEditorRanges.Edit(entity.BackgroundVisialization.RangeList[0].Coloring);
                    ctlColoringInfoEditorAutomatic.Edit(entity.BackgroundVisialization.Automatic.ColoringInfo);
                    ctlDefaultShapeColoring.Edit(entity.BackgroundVisialization.RangeList.DefaulColoringInfo);
                }
            }
        }

        public override ShapeItemVisualization GetEntity()
        {
            Current.BackgroundVisialization.DataType = (DataType)Enum.Parse(typeof(DataType), ctlRangeDataType.SelectedItem.Text);
            try
            {
                Current.BackgroundVisialization.RangeList.ConvertRanges();
            }
            catch (Exception ex)
            {
                throw new BusinessException(ex.Message.ToString());
            }
            //Current.BackgroundVisialization.RangeList.DefaulColor = (System.Drawing.KnownColor)Enum.Parse(typeof(System.Drawing.KnownColor), ctlDefaultColor.SelectedItem.Text);            
            Current.BackgroundVisialization.Mode = (ShapeBackgroundVisualizationMode)Enum.Parse(typeof(ShapeBackgroundVisualizationMode), ctlColoringType.SelectedItem.Text);
            Current.BackgroundVisialization.Automatic.ColoringInfo = ctlColoringInfoEditorAutomatic.EndEdit();
            Current.BackgroundVisialization.RangeList.DefaulColoringInfo = ctlDefaultShapeColoring.EndEdit();
            SaveEntity();
            return Current;

        }

        public override void ClearEditor()
        {
            ctlColoringInfoEditorRanges.ClearEditor();
            ctlRangeEnd.Text = string.Empty;
            ctlRangeStart.Text = string.Empty;
        }

        private void showSelectedEntity(int index, ShapeItemVisualization entity)
        {
            ShapeBackgroundRange xrange = entity.BackgroundVisialization.RangeList[index];
            ctlRangeStart.Text = xrange.Start.ToString();
            ctlRangeEnd.Text = xrange.Finish.ToString();
            ctlColoringInfoEditorRanges.Edit(xrange.Coloring);
        }

        protected void ctlRangeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            showSelectedEntity(ctlRangeList.SelectedIndex, Current);
        }

        protected void ctlCreateNewRange_Click(object sender, EventArgs e)
        {
            ctlColoringInfoEditorRanges.ClearCurrent();
            ShapeBackgroundRange xrange = new ShapeBackgroundRange();
            xrange.Coloring = ctlColoringInfoEditorRanges.EndEdit();
            xrange.Finish = ctlRangeEnd.Text.Trim();
            xrange.Start = ctlRangeStart.Text.Trim();
            Current.BackgroundVisialization.RangeList.Add(xrange);
            SaveEntity();
            ctlRangeList.Items.Add(ctlRangeStart.Text.Trim() + " - " + ctlRangeEnd.Text.Trim());
            ctlRangeList.SelectedIndex = ctlRangeList.Items.Count - 1;
        }

        protected void ctlDeleteRange_Click(object sender, EventArgs e)
        {
            int xindex = ctlRangeList.SelectedIndex;
            if (xindex < 0)
            {
                BusinessException xex = new BusinessException("You must select a range to delete");
                throw xex;
            }
            ctlRangeList.Items.RemoveAt(xindex);
            Current.BackgroundVisialization.RangeList.RemoveAt(xindex);
            base.SaveEntity();
            ClearEditor();
        }

        protected void ctlSave_Click(object sender, EventArgs e)
        {
            int xindex = ctlRangeList.SelectedIndex;
            if (xindex < 0)
            {
                BusinessException xex = new BusinessException("You must select a range to update");
                throw xex;
            }
            Current.BackgroundVisialization.RangeList[xindex].Coloring = ctlColoringInfoEditorRanges.EndEdit();
            Current.BackgroundVisialization.RangeList[xindex].Start = ctlRangeStart.Text.Trim();
            Current.BackgroundVisialization.RangeList[xindex].Finish = ctlRangeEnd.Text.Trim();
            SaveEntity();
            ShowEntity();


        }


        protected void ctlClearButton_Click(object sender, EventArgs e)
        {
            ClearEditor();
            ctlRangeList.Items.Clear();
            ClearCurrent();
        }






    }
}